package com.kzc.controller;

import com.kzc.base.model.Result;
import com.kzc.entity.Student;
import com.kzc.service.StudentService;
import com.kzc.vo.StudentVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 控制层
 * Author：柯尊诚
 * Created by anyleKe on 2016/1/29.
 */

@Controller
@RequestMapping("/")
public class StudentController {

//    Logger logger = LoggerFactory.getLogger(StudentController.class);//日志输出


    @Autowired
    private StudentService studentService;

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@ModelAttribute Student student) {
        int i = studentService.save(student);
        if (i <= 0) {
            return new Result(500, "新增数据失败!");
        } else {
            return new Result(200, student, "新增数据成功!");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@ModelAttribute Student student) {
        int i = studentService.update(student);
        if (i <= 0) {
            return new Result(500, "更新数据失败！");
        } else {
            return new Result(200, student, "更新数据成功！");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(int id) {
        int i = studentService.delete(id);
        if (i <= 0) {
            return new Result(500, "删除数据失败！");
        } else {
            return new Result(200, "删除成功！");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Object query() {
//        logger.debug("{}","nnnnnnnnnnnnnnnnnnnnnnnnn");
        return new Result(200, studentService.query(), "查询成功");
    }

    @ResponseBody
    @RequestMapping(value = "/queryPage", method = RequestMethod.GET)
    public Object queryPage(@ModelAttribute StudentVO vo) {
        List<Student> students = studentService.queryPage(vo);
        return new Result(200, students, vo.getPaging(), "请求成功");
    }

//    @ResponseBody
//    @RequestMapping()
//    public Object query() {
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("message", "aaa");
//        map.put("name", "aaa");
//        map.put("name", "aaa");
//        return map;
//    }
}
